﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "ImageStickerItemView.h"
#include "../Util/GDUtil.h"

ImageStickerItemView* ImageStickerItemView::createWithFrame(const CCRect& rect, GdBean& imageStickerBean)
{
	ImageStickerItemView * pRet = new ImageStickerItemView(imageStickerBean);
	if (pRet && pRet->initWithFrame(rect))
	{
		pRet->initView();
		pRet->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pRet);
	}
	return pRet;
}

ImageStickerItemView::ImageStickerItemView(GdBean& imageStickerBean)
:imageStickerBean(imageStickerBean)
, isClickMark(false)
, canMove(true)
, btnView(NULL)
, bgImage(NULL)
, fullBtn(NULL)
, closeBtn(NULL)
, ctlBtn(NULL)
, ctlImage(NULL)
, imageView(NULL)
, imageKuang(NULL)
, closeImage(NULL)
{
}


ImageStickerItemView::~ImageStickerItemView(void)
{
}

void ImageStickerItemView::initView()
{
	space = _ds(10);
	winSize = CCSize(_ds(320), _ds(140));
	minSize = CCSize(_ds(160), _ds(70));

	this->setColor(CAColor_clear);
	//if(IS_DEBUG) this->setColor(CAColor_red);
	this->setContentSize(winSize);

	imageView = CAView::createWithFrame(this->getBounds(), CAColor_clear);
	this->addSubview(imageView);

	bgImage = CAImageView::createWithFrame(CCRect(space, space, winSize.width - space * 2, winSize.height - space * 2));
	bgImage->setImage(CAImage::create(GD_IMAGE_PATH + imageStickerBean.value));
	imageView->addSubview(bgImage);

	imageKuang = CAScale9ImageView::createWithFrame(bgImage->getBounds());
	imageKuang->setImage(CAImage::create(GD_IMAGE_PATH + "goodsgrab/kuang_filter_select.png"));
	bgImage->addSubview(imageKuang);

	ctlImage = CAImageView::createWithFrame(CCRect(winSize.width - _ds(30), winSize.height - _ds(30), _ds(30), _ds(30)));
	ctlImage->setImage(CAImage::create(GD_IMAGE_PATH + "upload_fresh.png"));
	imageView->addSubview(ctlImage);

	closeImage = CAImageView::createWithFrame(CCRect(0, 0, _ds(30), _ds(30)));
	closeImage->setImage(CAImage::create(GD_IMAGE_PATH + "close_icon.png"));
	imageView->addSubview(closeImage);

	btnView = CAView::createWithFrame(this->getBounds(), CAColor_clear);
	this->addSubview(btnView);

	fullBtn = CAButton::createWithFrame(this->getBounds(), CAButtonTypeCustom);
	//fullBtn->setBackGroundViewForState(CAControlStateAll,CAView::createWithColor(CAColor_red));
	btnView->addSubview(fullBtn);
	fullBtn->setTouchEventScrollHandOverToSuperview(false);
	fullBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onFullBtnTouchMoved), CAControlEventTouchMoved);
	fullBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onFullBtnTouchMovedOutSide), CAControlEventTouchMovedOutSide);
	fullBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onFullBtnTouchDown), CAControlEventTouchDown);
	fullBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onFullBtnTouchUpInSide), CAControlEventTouchUpInSide);
	fullBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onFullBtnTouchUpOutSide), CAControlEventTouchUpOutSide);

	closeBtn = CAButton::createWithFrame(CCRect(0,0,_ds(30),_ds(30)), CAButtonTypeCustom);
	//closeBtn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_orange));
	closeBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCloseBtnClick), CAControlEventTouchUpInSide);
	btnView->addSubview(closeBtn);

	ctlBtn = CAButton::createWithFrame(CCRect(winSize.width - _ds(30), winSize.height - _ds(30), _ds(30), _ds(30)), CAButtonTypeCustom);
	//ctlBtn->setBackGroundViewForState(CAControlStateAll, CAView::createWithColor(CAColor_green));
	btnView->addSubview(ctlBtn);
	ctlBtn->setTouchEventScrollHandOverToSuperview(false);
	ctlBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCtlBtnTouchMoved), CAControlEventTouchMoved);
	ctlBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCtlBtnTouchMovedOutSide), CAControlEventTouchMovedOutSide);
	ctlBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCtlBtnTouchDown), CAControlEventTouchDown);
	ctlBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCtlBtnTouchUpInSide), CAControlEventTouchUpInSide);
	ctlBtn->addTarget(this, CAControl_selector(ImageStickerItemView::onCtlBtnTouchUpOutSide), CAControlEventTouchUpOutSide);
}

void ImageStickerItemView::setCanEdit(bool edit)
{
	fullBtn->setTouchEnabled(edit);
	closeBtn->setTouchEnabled(edit);
	ctlBtn->setTouchEnabled(edit);
	imageKuang->setVisible(edit);
	ctlImage->setVisible(edit);
	closeImage->setVisible(edit);
}

void ImageStickerItemView::onCloseBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point){
	
}

void ImageStickerItemView::onFullBtnTouchMoved(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	if (!canMove){
		return;
	}
	
	isClickMark = false;
	CCPoint point1 = fullBtn->getCenterOrigin(); // 坐标

	CCPoint newPoint = CCPoint(point1.x + point.x - touchPoint.x  , point1.y + point.y - touchPoint.y);
	//if (IS_DEBUG) CCLog("调整位置：[%f,%f]", newPoint.x, newPoint.y);
	imageView->setCenterOrigin(newPoint);
	//this->setFrameOrigin(newPoint);

	if (IS_DEBUG) CCLog("%s toumoved x:[%f,y:%f],newPoint:[%f,%f]", __FUNCTION__, point.x, point.y,newPoint.x, newPoint.y);
}

void ImageStickerItemView::onFullBtnTouchMovedOutSide(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	//if (IS_DEBUG) CCLog("%s toumoved x:%f,y:%f", __FUNCTION__, point.x, point.y);
	onFullBtnTouchMoved(control, point);
}
void ImageStickerItemView::onFullBtnTouchDown(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	if (!canMove){
		return;
	}
	//if (IS_DEBUG) CCLog("%s toumoved x:%f,y:%f", __FUNCTION__, point.x, point.y);
	touchPoint = point;
	isClickMark = true;
}
void ImageStickerItemView::onFullBtnTouchUpInSide(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	//if (IS_DEBUG) CCLog("%s toumoved x:%f,y:%f", __FUNCTION__, point.x, point.y);
	onFullBtnTouchUpOutSide(control, point);
}
void ImageStickerItemView::onFullBtnTouchUpOutSide(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	if (!canMove){
		return;
	}
	CCPoint centerPoint = CCPoint(this->getBounds().size.width / 2, this->getBounds().size.height / 2);

	CCPoint point1 = GDUtil::getRotationAnglePoint(centerPoint, touchPoint, this->getRotation()); // 按下的点
	CCPoint point2 = GDUtil::getRotationAnglePoint(centerPoint, point, this->getRotation()); // 按下的点

	//point1 = GDUtil::getRotationAnglePoint(CCPointZero, this->getCenterOrigin() + point1 - centerPoint, 0); // 按下的点
	//point2 = GDUtil::getRotationAnglePoint(CCPointZero, this->getCenterOrigin() + point2 - centerPoint, 0); // 抬起的点
	//if (IS_DEBUG) CCLog("%s point1 x:【%f,y:%f】,point2:【%f,y:%f】", __FUNCTION__, point1.x, point1.y, point2.x, point2.y);
	
	point1 = this->getCenterOrigin() + point1 - centerPoint; // 按下的点
	point2 = this->getCenterOrigin() + point2 - centerPoint; // 抬起的点

	this->setCenterOrigin(this->getCenterOrigin() + (point2 - point1));
	imageView->setFrameOrigin(CCPointZero);
}

void ImageStickerItemView::adjustBounce(const CCSize& viewSize)
{
	return;
	CCPoint point = this->getFrameOrigin();

	point.x = max(point.x, _ds(5));
	point.x = min(point.x, viewSize.width - (this->getBounds().size.width + _ds(5)));
	point.y = max(point.y, _ds(5));
	point.y = min(point.y, viewSize.height - _ds(5) - this->getBounds().size.height);

	this->setFrameOrigin(point);
}

GdBean ImageStickerItemView::getImageStickerBean()
{
	return imageStickerBean;
}

void ImageStickerItemView::onCtlBtnTouchMoved(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	;
	float dx = getCtlToCenterDistance(point) - getCtlToCenterDistance(ctlPoint);

	//if (IS_DEBUG) CCLog("%s x:%f,y:%f,缩放dx:%f", __FUNCTION__, point.x, point.y,dx);
	
	dtImageSize(dx);

	float baseAngle = getCtlToCenterAngle(ctlPoint);
	float newAngle = getCtlToCenterAngle(point);

	//if (IS_DEBUG) CCLog("原来角度%f,新角度%f", baseAngle, newAngle);
	imageView->setRotation(newAngle - baseAngle);
}
void ImageStickerItemView::onCtlBtnTouchMovedOutSide(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	onCtlBtnTouchMoved(control, point);
}
void ImageStickerItemView::onCtlBtnTouchDown(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	canMove = false;
	ctlPoint = point;
}
void ImageStickerItemView::onCtlBtnTouchUpInSide(CrossApp::CAControl *control, CrossApp::CCPoint point){
	canMove = true;
	fixViewSize();
}
void ImageStickerItemView::onCtlBtnTouchUpOutSide(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	onCtlBtnTouchUpInSide(control, point);
}

void ImageStickerItemView::dtImageSize(float dt)
{
	
	CCSize curSize = this->getBounds().size;
	//float r = getCtlToCenterAngle(CCPoint(curSize.width - _ds(15), curSize.height - _ds(15)));
	//dt = dt - r;

	if (curSize.width + dt*2 < minSize.width){ // 小于最小尺寸了
		return;
	}
	if (curSize.width + dt * 2 > this->getSuperview()->getBounds().size.width){ // 大于最大尺寸了
		return;
	}
	CCRect imageViewRect = CCRect( - dt,  - dt, curSize.width, curSize.height);
	
	float width = curSize.width + dt * 2;
	float height = width * winSize.height / winSize.width;
	imageViewRect.origin.y = (curSize.height - height) / 2;
	imageViewRect.size.width = width;
	imageViewRect.size.height = height;

	imageView->setFrame(imageViewRect);
	bgImage->setFrame(CCRect(space, space,width - space * 2, height - space * 2));
	imageKuang->setFrame(bgImage->getBounds());

	ctlImage->setFrameOrigin(CCPoint(imageViewRect.size.width - _ds(30), imageViewRect.size.height - _ds(30)));
	//fullBtn->setFrame(CCRect(space, space, width - space * 2, height - space * 2));
	//ctlBtn->setFrameOrigin(CCPoint(width - ctlBtn->getBounds().size.width,height-ctlBtn->getBounds().size.height));
}

void ImageStickerItemView::fixViewSize()
{
	//if (IS_DEBUG) CCLog("旋转前坐标：[%f,%f]",this->getFrameOrigin().x,this->getFrameOrigin().y);
	CCRect newRect = CCRect(this->getFrameOrigin().x + imageView->getFrameOrigin().x, this->getFrameOrigin().y + imageView->getFrameOrigin().y, imageView->getBounds().size.width, imageView->getBounds().size.height);
	this->setFrame(newRect);
	imageView->setFrameOrigin(CCPointZero);

	btnView->setFrame(this->getBounds());
	fullBtn->setFrame(CCRect(space, space, newRect.size.width - space * 2, newRect.size.height - space * 2));
	ctlBtn->setFrame(CCRect(newRect.size.width - _ds(30), newRect.size.height - _ds(30), _ds(30), _ds(30)));
	this->setRotation(this->getRotation() + imageView->getRotation());
	imageView->setRotation(0);
	//if (IS_DEBUG) CCLog("旋转后坐标：[%f,%f]", this->getFrameOrigin().x, this->getFrameOrigin().y);
}

float ImageStickerItemView::getCtlToCenterAngle(CCPoint ctlPoint)
{
	// 中心点坐标
	CCPoint centPoint = CCPoint(this->getBounds().size.width / 2, this->getBounds().size.height / 2);
	// 控制点真实坐标
	CCPoint ctrNewPoint = CCPoint(this->getBounds().size.width - _ds(30) + ctlPoint.x, this->getBounds().size.height - _ds(30) + ctlPoint.y);

	float x = centPoint.x - ctrNewPoint.x;
	float y = centPoint.y - ctrNewPoint.y;

	float angle = GDUtil::getAngle(x, y);
	//CCLog("角度：%f", angle);
	return angle;
}

float ImageStickerItemView::getCtlToCenterDistance(CCPoint ctlPoint)
{
	// 中心点坐标
	CCPoint centPoint = CCPoint(this->getBounds().size.width / 2, this->getBounds().size.height / 2);
	// 控制点真实坐标
	CCPoint ctrNewPoint = CCPoint(this->getBounds().size.width - _ds(30) + ctlPoint.x, this->getBounds().size.height - _ds(30) + ctlPoint.y);

	return sqrtf((centPoint.x - ctrNewPoint.x)*(centPoint.x - ctrNewPoint.x) + (centPoint.y - ctrNewPoint.y)*(centPoint.y - ctrNewPoint.y));
}